package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemMenuButtonDao;
import com.example.child_protection.dao.SystemMenuDao;
import com.example.child_protection.dto.info.SystemMenuButtonDTO;
import com.example.child_protection.dto.info.SystemMenuDTO;
import com.example.child_protection.dto.info.SystemMenuDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemMenuRequestDTO;
import com.example.child_protection.dto.request.find.SystemRoleMenuRequestDTO;
import com.example.child_protection.dto.response.*;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemMenuButtonModel;
import com.example.child_protection.model.SystemMenuModel;
import com.example.child_protection.service.SystemMenuQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单信息查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemMenuQueryServiceImpl extends MyBaseService implements SystemMenuQueryService {

    @Autowired
    public SystemMenuDao systemMenuDao;

    @Autowired
    public SystemMenuButtonDao departmentDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemMenuResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_MENU_FIND_ID, requestDTO, SystemMenuResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemMenuResponseDTO>() {
            @Override
            protected SystemMenuResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemMenuResponseDTO responseDTO = new SystemMenuResponseDTO();
                SystemMenuModel systemMenuModel = systemMenuDao.findId(requestDTO.getId());
                SystemMenuDTO systemMenu = new SystemMenuDTO();
                BeanUtils.copyProperties(systemMenuModel, systemMenu);
                responseDTO.setInfo(systemMenu);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemMenuListResponseDTO findList(SystemMenuRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_MENU_FIND_LIST, requestDTO, SystemMenuListResponseDTO.class, new MyServiceExecutor<SystemMenuRequestDTO, SystemMenuListResponseDTO>() {
            @Override
            protected SystemMenuListResponseDTO myprocess(SystemMenuRequestDTO requestDTO) throws Exception {
                SystemMenuListResponseDTO responseDTO = new SystemMenuListResponseDTO();
                SystemMenuModel req = new SystemMenuModel();
                BeanUtils.copyProperties(requestDTO, req);
                req.setIsRoot(BooleanEnums.T);
                List<SystemMenuModel> lists = systemMenuDao.findPage(req);
                List<SystemMenuDTO> menuInfos = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemMenuDTO.class);

                if (requestDTO.getIsOnlyRoot().equals(BooleanEnums.F)) {
                    for (SystemMenuDTO sm : menuInfos) {
                        sm.setButtons(getButtons(sm.getId()));
                        sm.setChildMenus(getChildMenus(sm.getCode()));
                    }
                }

                responseDTO.setInfos(menuInfos);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 角色菜单列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemMenuListResponseDTO findRoleMenuList(SystemMenuRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_MENU_FIND_ROLE_MENU_LIST, requestDTO, SystemMenuListResponseDTO.class, new MyServiceExecutor<SystemMenuRequestDTO, SystemMenuListResponseDTO>() {
            @Override
            protected SystemMenuListResponseDTO myprocess(SystemMenuRequestDTO requestDTO) throws Exception {
                SystemMenuListResponseDTO responseDTO = new SystemMenuListResponseDTO();
                SystemMenuModel req = new SystemMenuModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemMenuModel> lists = systemMenuDao.findList(req);
                List<SystemMenuDTO> menuInfos = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemMenuDTO.class);
                List<SystemMenuDTO> childMenuInfos = null;
                for (SystemMenuDTO sm : menuInfos) {
                    childMenuInfos = new ArrayList<>();
                    childMenuInfos = getChildMenus(sm.getCode());
                    /*for (SystemMenuDTO child : childMenuInfos) {
                        child.setChildMenus(getChildMenus(sm.getCode()));
                    }*/
                    sm.setChildMenus(childMenuInfos);
                }
                responseDTO.setInfos(menuInfos);
                return responseDTO;
            }
        });
    }

    public List<SystemMenuDTO> getChildMenus(String parentCode) {
        SystemMenuModel req = new SystemMenuModel();
        req.setParentCode(parentCode);
        List<SystemMenuModel> listsModel = systemMenuDao.findPage(req);
        List<SystemMenuDTO> menus = JSONObject.parseArray(JSONArray.toJSONString(listsModel), SystemMenuDTO.class);

        for (SystemMenuDTO sm : menus) {
            sm.setButtons(getButtons(sm.getId()));
        }
        return menus;
    }


    public List<SystemMenuButtonDTO> getButtons(Long menuId) {
        List<SystemMenuButtonDTO> roles= new ArrayList<>();
        SystemMenuButtonModel req = new SystemMenuButtonModel();
        req = new SystemMenuButtonModel();
        req.setMenuId(menuId);
        roles = JSONObject.parseArray(JSONArray.toJSONString(departmentDao.findPage(req)), SystemMenuButtonDTO.class);
        return roles;
    }
}
